package com.gientech.controller;

import com.gientech.entity.CommonResult;
import com.gientech.entity.LoanInfo;
import com.gientech.service.LoanInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 贷款业务流程处理
 */
@RestController
@RequestMapping("/task")
public class LoanInfoController {

    private static final Logger logger = LoggerFactory.getLogger(LoanInfoController.class);

    @Autowired
    private LoanInfoService loanInfoService;

    /**
     * 申请贷款，发起贷款业务审批流程
     * @return
     */
    @RequestMapping("/applyLoan")
    public CommonResult applyLoan(@RequestBody LoanInfo loanInfo){
        logger.info("申请贷款，发起贷款业务审批流程>>>>>>>>>>>{}", loanInfo);
        // 发起贷款业务审批流程
        String taskId = loanInfoService.startTask();
        loanInfo.setTaskId(taskId);
        // 申请贷款信息写入数据库
        loanInfoService.insertLoanInfo(loanInfo);
        return CommonResult.success("");
    }

    /**
     * 查询用户的代办任务列表
     * @param userName
     * @return
     */
    @RequestMapping("/queryTask")
    public CommonResult queryTask(String userName){
        logger.info("查询用户的代办任务列表>>>>>>>>>>>{}", userName);
        List<LoanInfo> list = loanInfoService.queryTaskByUserName(userName);
        return CommonResult.success(list);
    }

    /**
     * 审批贷款
     *
     * @return
     */
    @RequestMapping("/checkApply")
    public CommonResult checkApply(@RequestBody LoanInfo loanInfo){
        logger.info("审批贷款>>>>>>>>>>>{}");
        loanInfoService.checkApply(loanInfo);
        return CommonResult.success("");
    }

    @RequestMapping("/testRocketMq")
    public void testRocketMq() throws Exception {
        LoanInfo loanInfo = new LoanInfo();
        loanInfo.setId(1);
        loanInfoService.sendLoanInfoToRisk(loanInfo);
    }

}
